import dayjs from "dayjs";

/**
 * 格式化
 */
const useFormat = () => {
  // 格式化日期
  const formatDate = (item: any) => {
    return dayjs(item);
  };

  // 每三位加逗号
  const formatCommas = (number: string | number) => {
    return number.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
  };

  // 姓名脱敏
  const formatName = (name: string) => {
    if (name.length == 2) {
      name = name.substring(0, 1) + "*"; // 截取name 字符串截取第一个字符，
      return name;
    } else if (name.length == 3) {
      name = name.substring(0, 1) + "*" + name.substring(2, 3); // 截取第一个和第三个字符
      return name;
    } else if (name.length > 3) {
      name = name.substring(0, 1) + "*" + "*" + name.substring(3, name.length); // 截取第一个和大于第4个字符
      return name;
    }
  };

  // 手机号脱敏
  const formatPhone = (str: string | number) => {
    return str.toString().replace(/^(.{3})(?:\d+)(.{4})$/, "$1****$2");
  };

  // 身份证号脱敏
  const formatIdCard = (str: string | number) => {
    return str.toString().replace(/^(.{6})(?:\d+)(.{2})$/, "$1**********$2");
  };

  return {
    formatName,
    formatDate,
    formatPhone,
    formatIdCard,
    formatCommas,
  };
};

export default useFormat;
